﻿using System;
using System.Windows.Forms;
using System.Drawing;
using uz.control.CustomControl;


namespace uz.control.IPO
{
    public class IPOProcessLine : TNB_Label
    {
        private bool _IsNew = true;

        public bool IsNew
        {
            get { return _IsNew; }
            set { _IsNew = value; }
        }

        public IPOProcessLine()
        {
            ToolBar.AddCommand(CMD_EDIT_STR, "编辑（F2）");
            ToolBar.AddCommand(CMD_DEL_STR, "删除（F3）");
            ToolBar.AddCommand(CMD_ADD_STR, "添加行（F4）");
            ToolBar.AddCommand(CMD_ADD_CHILD_STR, "插入行（F5）");
        }

        public override string ID
        {
            get
            {
                return Index.ToString();
            }
        }

        public override string Name
        {
            get
            {
                return Index.ToString();
            }
        }

        public override void DrawNodeText(PaintEventArgs e, Brush brush, Rectangle Bounds)
        {
            SizeF sf = SizeF.Empty;
            string str = this.Text;
            sf = GetDrawTextSizeF(e.Graphics, UzTreeView.Font, str);
            float xOffset = Bounds.Left + HandleAreaWidth + GetIndentWidth() + 0;
            float yOffset = Bounds.Top + 5;
            e.Graphics.DrawString(str, UzTreeView.Font, brush, xOffset, yOffset);
        }

        public override bool EndEdit(UzTextBox txt)
        {
            this.Text = txt.Text.TrimEnd();

            IsNew = false;

            UzTreeNode node = GetNextBrotherNode();

            if (node == null)
            {
                Add();
            }
            else
            {
                node.IsEdit = true;
            }

            return true;
        }

        public override bool IsEdit
        {
            get
            {
                return base.IsEdit;
            }
            set
            {
                base.IsEdit = value;
                if (value == false)
                {
                    if (IsNew == true)
                    {
                        Delete();
                    }
                }
            }
        }

        public override void BeginEdit(UzTextBox txt)
        {
            txt.Text = this.Text;
            txt.SelectAll();
        }


        public override void Delete()
        {
            UzTreeNode node = null;
            if (IsNew == false)
            {
                node = GetNextVisibleNode();
                if (node == null)
                {
                    node = GetPrevVisibleNode();
                }
            }
            else
            {
                node = GetPrevVisibleNode();
                if (node == null)
                {
                    node = GetNextVisibleNode();
                }
            }

            Parent.Nodes.RemoveAt(Index);

            if (node != null)
            {
                UzTreeView.SelectedNode = node;
            }

        }


        public override void Add()
        {
            IPOProcessLine node = new IPOProcessLine();

            Parent.Nodes.Add(node);
            node.IsEdit = true;

            if (IsExpanded == false)
            {
                Expand();
            }

        }

        public override UzTreeNode AddChild()
        {
            IPOProcessLine node = new IPOProcessLine();

            Parent.Nodes.Insert(Index, node);
            
            //Nodes.Add(node);
            node.IsEdit = true;

            if (IsExpanded == false)
            {
                Expand();
            }

            return node;
        }

        public override bool ProcessKeyDown(KeyEventArgs e)
        {
            if (e.KeyData == (Keys.Shift | Keys.Left))
            {
                if (Parent.Parent is IPOProcess || Parent.Parent is IPOProcessLine)
                {
                    Parent.Nodes.RemoveAt(Index);
                    Parent.Parent.Nodes.Insert(Parent.Index + 1, this);
                }
                return true;
            }
            else if (e.KeyData == (Keys.Shift | Keys.Right))
            {
                UzTreeNode node = GetPrevBrotherNode();

                if(node !=null)
                {
                    Parent.Nodes.RemoveAt(Index);
                    node.Nodes.Insert(node.Nodes.Count, this);
                }
            }
            else if (e.KeyCode == Keys.Enter)
            {
                if (IsEdit == false)
                {
                    IsEdit = true;
                }
            }

            return base.ProcessKeyDown(e);
        }
    }
}
